unit PagDuplicataPagRecordFr;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, DBCtrls, ActnList, Buttons, ComCtrls, ExtCtrls, EditBtn, DBGrids,
  MainRecordFr;

type

  { TPagDuplicataPagRecordFrame }

  TPagDuplicataPagRecordFrame = class(TMainRecordFrame)
    DBGrid1: TDBGrid;
    ContaLookup: TDBLookupComboBox;
    ValorPagoDBEdit: TDBText;
    GroupBox3: TGroupBox;
    Label10: TLabel;
    PlanoContaLookup: TDBLookupComboBox;
    Label9: TLabel;
    LocalizarBaixaAction: TAction;
    BtnDuplicata: TBitBtn;
    DataPagamentoDBedit: TDBEdit;
    DataVencimentoDBEDit: TDBEdit;
    DBEdit1: TDBEdit;
    DescontoDBEDit: TDBEdit;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    IdDuplicataDBEdit: TDBEdit;
    IdFornecedorDBEdit: TDBEdit;
    AcrescimoEdit: TDBEdit;
    Label1: TLabel;
    ToolButton13: TToolButton;
    ToolButton19: TToolButton;
    ToolButton20: TToolButton;
    ToolButton9: TToolButton;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    NomePessoaDBEDit: TDBEdit;
    observacaoDBEdit: TDBMemo;
    RecordPageControl: TPageControl;
    Panel14: TPanel;
    Panel15: TPanel;
    PainelTotal: TPanel;
    Panel17: TPanel;
    ParcelaDBEdit: TDBEdit;
    CancelarBaixa: TAction;
    ReceberBaixa: TAction;
    SalvarBaixa: TAction;
    ExcluirBaixa: TAction;
    FecharBaixa: TAction;
    DuplicataActionList: TActionList;
    status: TDBRadioGroup;
    DuplicataTabSheet: TTabSheet;
    ToolBar1: TToolBar;
    ToolButton14: TToolButton;
    ToolButton15: TToolButton;
    ToolButton16: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    Valor: TLabel;
    valor1: TDBText;
    procedure AcrescimoEditKeyPress(Sender: TObject; var Key: char);
    procedure BtnDuplicataClick(Sender: TObject);
    procedure CancelarBaixaExecute(Sender: TObject);
    procedure DescontoDBEDitKeyPress(Sender: TObject; var Key: char);
    procedure ExcluirBaixaExecute(Sender: TObject);
    procedure FecharBaixaExecute(Sender: TObject);
    procedure LocalizarBaixaActionExecute(Sender: TObject);
    procedure ReceberBaixaExecute(Sender: TObject);
    procedure SalvarBaixaExecute(Sender: TObject);
    procedure ControlaBotoes(); override;
    constructor Create(TheOwner: TComponent); override;

  private
    { private declarations }
  public
    { public declarations }
    IDCaixa: string;
  end;

implementation

uses Initializator, MainFr, DuplicataPagDM, MainDM, MainFm, ContaDM;

procedure TPagDuplicataPagRecordFrame.BtnDuplicataClick(Sender: TObject);
begin
  TheControl.ShowSearchFrame(TMainFrame(DuplicataPagSearchFrame));
end;

procedure TPagDuplicataPagRecordFrame.AcrescimoEditKeyPress(Sender: TObject;
  var Key: char);
var
  Acrescimo: real;
begin
  //! Verificar se é 13 para outros SO
  if (key = #13) then begin
    Acrescimo := StrToFloatDef(AcrescimoEdit.Text, 0);
     DuplicataPagData.DuplicataPagZQuery.FieldByName('valor_pago').AsString:= FloatToStr(StrToFloatDef(Valor1.Caption, 0) + Acrescimo);
  end;
end;

procedure TPagDuplicataPagRecordFrame.CancelarBaixaExecute(Sender: TObject);
begin
  DuplicataPagData.DuplicataPagZQuery.Cancel();
  DuplicataPagData.DuplicataPagZQuery.Connection.Rollback();
  ControlaBotoes();
end;

procedure TPagDuplicataPagRecordFrame.DescontoDBEDitKeyPress(Sender: TObject;
  var Key: char);
var
  Desconto: real;
begin
  if (Key = #13) then begin
    Desconto := StrToFloatDef(DescontoDBEDit.Text, 0);
    DuplicataPagData.DuplicataPagZQuery.FieldByName('valor_pago').AsString:= FloatToStr(StrToFloat(valor1.Caption) - Desconto);
  end;
end;

procedure TPagDuplicataPagRecordFrame.ExcluirBaixaExecute(Sender: TObject);
begin
  DuplicataPagData.CancelaDuplicata(StrToInt(IdDuplicataDBEdit.Text),
    StrToInt(ParcelaDBEdit.Text));
  ControlaBotoes();
end;

procedure TPagDuplicataPagRecordFrame.FecharBaixaExecute(Sender: TObject);
begin
  TheControl.BackFrame();
end;

procedure TPagDuplicataPagRecordFrame.LocalizarBaixaActionExecute(Sender: TObject);
begin
  TheControl.ShowSearchFrame(TMainFrame(DuplicataPagSearchFrame));
end;

procedure TPagDuplicataPagRecordFrame.ReceberBaixaExecute(Sender: TObject);
begin
  IDCaixa := DuplicataPagData.BuscaIDCaixa();
  if (IDCaixa = '') then begin
    if MessageDlg('Confirmação', 'Deseja abrir o caixa?', mtConfirmation,
      mbYesNo, 0) = mrYes then begin
      ControlaBotoes();
      TheControl.ShowFrame(TMainFrame(CaixaInicioRecordFrame));
    end else begin
      TheControl.BackFrame();
    end;
  end else begin
    if (IdDuplicataDBEdit.Text <> '') and (ParcelaDBEdit.Text <> '') then begin
      DuplicataPagData.DuplicataPagZQuery.Connection.StartTransaction();
      DuplicataPagData.DuplicataPagZQuery.Edit();
      DataPagamentoDBedit.Text := DuplicataPagData.BuscaData();
      DuplicataPagData.DuplicataPagZQuery.FieldByName('valor_pago').AsString:= valor1.Caption;
      ControlaBotoes();
    end;
  end;
end;

procedure TPagDuplicataPagRecordFrame.SalvarBaixaExecute(Sender: TObject);
var
  Desconto, Acrescimo: real;
begin
  if ContaLookup.Text = '' then begin
    ShowMessage('Selecione uma conta!');
  end else begin
    Desconto := StrToFloatDef(DescontoDBEDit.Text, 0);
    Acrescimo := StrToFloatDef(AcrescimoEdit.Text, 0);
    DuplicataPagData.DuplicataPagZQuery.Post();
    DuplicataPagData.InsereBaixaDuplicata(StrToInt(ParcelaDBEdit.Text),
      StrToDate(DataPagamentoDBedit.Text),
      Desconto, Acrescimo, StrToFloatDef(ValorPagoDBEdit.Caption, 0),
      StrToInt(IdDuplicataDBEdit.Text));
    DuplicataPagData.InsereBaixaConta(ValorPagoDBEdit.Caption, ContaLookup.KeyValue,
      PlanoContaLookup.KeyValue, IntToStr(MainForm.Usuario), IDCaixa);
    DuplicataPagData.DuplicataPagZQuery.Connection.Commit();
    ContaData.ContaZQuery.Refresh();
    ControlaBotoes();
  end;
end;

procedure TPagDuplicataPagRecordFrame.ControlaBotoes();
var
  I: integer;
begin
  // Desabilita todas as ações
  for I := 0 to RecordActionList.ActionCount - 1 do begin
    TAction(RecordActionList.Actions[I]).Enabled := False;
  end;
  for I := 0 to DuplicataActionList.ActionCount - 1 do begin
    TAction(DuplicataActionList.Actions[I]).Enabled := False;
  end;
  RecordDBNavigator.Enabled := False;
  // Verifica se o frame possui módulo de dados
  if (DuplicataPagData.DataSetState() = mdsDisable) then begin
    // Se não tiver desabilita todos os componentes
    for I := 0 to Self.ComponentCount - 1 do
      if Self.Components[I] is TControl then
        TControl(Self.Components[I]).Enabled := False;
  end else begin
    // Verifica se o DataSet está em modo de edição e controla habilitação das ações
    if (DuplicataPagData.DataSetState() in [mdsEditing, mdsInserting]) then begin
      SalvarBaixa.Enabled := True;
      CancelarBaixa.Enabled := True;
    end else begin
      LocalizarBaixaAction.Enabled := True;
      FecharBaixa.Enabled := True;
      if (DuplicataPagData.DataSetState() = mdsCanEdit) then begin
        ExcluirBaixa.Enabled := True;
        ReceberBaixa.Enabled := True;
      end;
    end;
  end;
end;

constructor TPagDuplicataPagRecordFrame.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  RecordPageControl.ActivePage := DuplicataTabSheet;
end;

{ TPagDuplicataPagRecordFrame }


initialization
  {$I pagduplicatapagrecordfr.lrs}

end.
